package com.outsourcing.bottle.ui;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.UICore;
/**
 * 
 * @author 06peng
 *
 */
public class MoreActivity extends BasicActivity implements BasicUIEvent, OnClickListener, Callback {
	
	private final int get_more_user_info = 1;

	private RelativeLayout homeLayout;
	private RelativeLayout friendFeedLayout;
	private RelativeLayout inviteLayout;
//	private RelativeLayout myspaceLayout;
	private RelativeLayout myablumsLayout;
	private RelativeLayout settingLayout;
	private RelativeLayout notificationLayout;
	private RelativeLayout messageLayout;
	private RelativeLayout myfriendLayout;
	private RelativeLayout maybekwonLayout;
	
	private ImageView backView;
	private ImageView homeView;
	private Button notificationCount;
	private Button messageCount;
	private Button friendfeedsCount;
	private Button maybekwonCount;
	
	private int new_feedcount;
	private int new_msgcount;
	private int new_maybeknowcount;
	private int new_noticecount;
	
	private Handler handler;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		handler = new Handler(this);
		setContentView(R.layout.more);
		initWidget();
		UICore.eventTask(this, this, get_more_user_info, "", null);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	private void updateUI() {
		if (new_msgcount > 0) {
			messageCount.setVisibility(View.VISIBLE);
			messageCount.setText(new_msgcount + "");
		} else {
			messageCount.setVisibility(View.GONE);
		}
		if (new_noticecount > 0) {
			notificationCount.setVisibility(View.VISIBLE);
			notificationCount.setText(new_noticecount + "");
		} else {
			notificationCount.setVisibility(View.GONE);
		}
		if (new_maybeknowcount > 0) {
			maybekwonCount.setVisibility(View.VISIBLE);
			maybekwonCount.setText(new_maybeknowcount + "");
		} else {
			maybekwonCount.setVisibility(View.GONE);
		}
		if (new_feedcount > 0) {
			friendfeedsCount.setVisibility(View.VISIBLE);
			friendfeedsCount.setText(new_feedcount + "");
		} else {
			friendfeedsCount.setVisibility(View.GONE);
		}
	}
	
	private void initWidget() {
		homeLayout = (RelativeLayout) findViewById(R.id.more_home_layout);
		homeLayout.setOnClickListener(this);
		friendFeedLayout = (RelativeLayout) findViewById(R.id.more_friends_feed_layout);
		friendFeedLayout.setOnClickListener(this);
		friendfeedsCount = (Button) findViewById(R.id.more_friendfeeds_count);
		inviteLayout = (RelativeLayout) findViewById(R.id.more_bind_invite_layout);
		inviteLayout.setOnClickListener(this);
//		myspaceLayout = (RelativeLayout) findViewById(R.id.more_myspace_layout);
//		myspaceLayout.setOnClickListener(this);
		myablumsLayout = (RelativeLayout) findViewById(R.id.more_albums_layout);
		myablumsLayout.setOnClickListener(this);
		settingLayout = (RelativeLayout) findViewById(R.id.more_setting_layout);
		settingLayout.setOnClickListener(this);
		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		homeView = (ImageView) findViewById(R.id.topbar_menu);
		homeView.setOnClickListener(this);
		notificationLayout = (RelativeLayout) findViewById(R.id.more_notification_layout);
		notificationLayout.setOnClickListener(this);
		notificationCount = (Button) findViewById(R.id.more_notification_count);
		messageLayout = (RelativeLayout) findViewById(R.id.more_message_layout);
		messageLayout.setOnClickListener(this);
		messageCount = (Button) findViewById(R.id.more_message_count);
		myfriendLayout = (RelativeLayout) findViewById(R.id.more_myfriend_layout);
		myfriendLayout.setOnClickListener(this);
		maybekwonLayout = (RelativeLayout) findViewById(R.id.more_maybekown_layout);
		maybekwonLayout.setOnClickListener(this);
		maybekwonCount = (Button) findViewById(R.id.more_maybekown_count);
	}
	
	@Override
	public void onClick(View v) {
		if (v == backView) {
			finish();
		} else if (v == settingLayout) {
			Intent settingIntent = new Intent(this, SettingActivity.class);
			startActivity(settingIntent);
		} else if (v == inviteLayout) {
			Intent intent = new Intent(this, BindInviteActivity.class);
			startActivity(intent);
		} else if (v == homeLayout) {
			Intent intent = new Intent(this, HomeActivity.class);
			intent.putExtra("currentItem", 1);
			startActivity(intent);
		}
//		else if (v == myspaceLayout) {
//			Intent intent = new Intent(this, UserSpaceActivity.class);
//			intent.putExtra("ouid", AppContext.getInstance().getLogin_uid());
//			startActivity(intent);
//		} 
		else if (v == friendFeedLayout) {
			Intent intent = new Intent(this, FriendFeedActivity.class);
			startActivity(intent);
		} else if (v == myablumsLayout) {
			Intent intent = new Intent(this, AlbumsActivity.class);
			intent.putExtra("ouid", AppContext.getInstance().getLogin_uid());
			startActivity(intent);
		} else if (v == homeView) {
			Intent intent = new Intent(MoreActivity.this, HomeActivity.class);
			intent.putExtra("currentItem", 1);
			startActivity(intent);
			finish();	
		} else if (v == notificationLayout) {
			Intent intent = new Intent(this, NoticeListActivity.class);
			startActivity(intent);
		} else if (v == messageLayout) {
			Intent intent = new Intent(this, MessageListActivity.class);
			startActivity(intent);
		} else if (v == myfriendLayout) {
			Intent intent = new Intent(this, HomeActivity.class);
			AppContext.getInstance().setCurrentItem(2);
			AppContext.getInstance().setGoToMyfriend(true);
			startActivity(intent);
		} else if (v == maybekwonLayout) {
			Intent intent = new Intent(this, HomeActivity.class);
			AppContext.getInstance().setCurrentItem(2);
			AppContext.getInstance().setGotoMaybeKown(true);
			startActivity(intent);
		}
	}
	
	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case get_more_user_info:
			getMoreUserInfo();
			handler.sendEmptyMessage(get_more_user_info);
			break;

		default:
			break;
		}
	}
	
	private void getMoreUserInfo() {
		StringBuffer url = new StringBuffer(UrlConfig.get_more_info);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				JSONObject obj = new JSONObject(result);
				JSONObject resultObj = obj.getJSONObject("more_info");
				new_feedcount = resultObj.getInt("new_feedcount");
				new_msgcount = resultObj.getInt("new_msgcount");
				new_maybeknowcount = resultObj.getInt("new_maybeknowcount");
				new_noticecount = resultObj.getInt("new_noticecount");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case get_more_user_info:
			updateUI();
			break;

		default:
			break;
		}
		return false;
	}

}
